﻿using System;
using System.Linq;
using Project.Data;
using Project.Data.Repository;
using Project.Model.EF6.Entity;
using Project.Services.Tools;
using System.Collections.Generic;
using System.Globalization;
using Project.Model.EF6.ViewModel;

namespace Project.Services
{
    public static class TestService
    {
        public static string LinqTest()
        {
            using (var db = new EfContext())
            {
                string text = db.ActiveConfig.FirstOrDefault().ToJson();
                return text;
            }
        }

        public static string RepositoryTest()
        {
            try
            {
                var db = new DbContextRepository<EfContext>();
                return db.Find<SalesRecord>(n => n.QR_CODE == "0223828785656675").ToJson();
            }
            catch (Exception ex)
            {
                ex.ErrorHandle();
                return "";
            }
        }

        public static int TestData()
        {
            int count = 0;
            var yesterday = DateTime.Parse("2017-11-14");
            var rep = new DbContextRepository<EfContext>();
            var distributor = rep.FindAll<Distributor>(); //经销商
                var distriView =
                    distributor.Select(m => new { m.DISCODE, m.DISNAME, m.POVINCIAL, m.CITY }).ToList();
                var product = rep.FindAll<Product>();//所有产品数据
                string[] productType = { "01", "04", "03", "1" };//产品分类
                var productList = product.Select(m => new { m.PRODUCT_ID, m.PRODUCT_NAME, m.PRODUC_SPE_ID });
                var provinceList = distriView.Select(m => m.POVINCIAL).Distinct().ToList();
                var saleIdList = distriView.Select(m => m.DISCODE).Distinct().ToList();
                var dayOfWeek = (int)yesterday.DayOfWeek;
                //获取今天是今年第几周
                GregorianCalendar gc = new GregorianCalendar();
                int weekOfYear = gc.GetWeekOfYear(yesterday, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
                bool isFirstDay = dayOfWeek == 1;

                var firstDayOfMonth = DateTime.Today.AddDays(1 - DateTime.Today.Day);
                bool isFirstDayOfMonth = firstDayOfMonth == yesterday;
                var currMonth = yesterday.Month;

                #region 48个半点统计
                var dayPeopleSemih = new List<PeopleSemih>(); //每天48个半点红包参与人数
                var dayOpenid = new List<OpenIdViewModel>();
                for (int i = 0; i < 48; i++)
                {
                    var start = yesterday.AddMinutes(i * 30);
                    var end = yesterday.AddMinutes((i + 1) * 30);
                    //var list = rep.FindAll<SalesRecord>(m => m.ROW_CREATE_DATE > start && m.ROW_CREATE_DATE <= end);
                    var list =
                        rep.QueryList<SalesViewModel>(
                            "Select ID,SALEID,REDPKT_AMOUNT,OPENID,POINT,PRODUC_SPE_ID,PRODUCT_ID From T_SGM_XLW_SALES_RECORD Where ROW_CREATE_DATE > TO_DATE('" +
                            start + "','yyyy-mm-dd hh24:mi:ss') And ROW_CREATE_DATE <=TO_DATE('" + end +
                            "','yyyy-mm-dd hh24:mi:ss')");
                    
                    var peopleSemih = new List<PeopleSemih>(); //半小时参与人数
                    #region 省份统计
                    foreach (var n in provinceList)
                    {
                        var distribu = distriView.Where(m => m.POVINCIAL == n).ToList(); //当前省份的经销商
                        var discode = distribu.Select(m => m.DISCODE);
                        var sales = list.Where(m => discode.Contains(m.SALEID)).ToList();
                        dayOpenid.AddRange(sales.Select(r => new OpenIdViewModel
                        {
                            Provice = n,
                            OpenId = r.OPENID
                        }));
                       
                     
                        var people = new PeopleSemih
                        {
                            PROVINCE = n,
                            TYPE = i + 1,
                            COUNT = sales.Select(m => m.OPENID).Distinct().Count(),
                            ROW_UPDATE_DATE = yesterday
                        };
                        peopleSemih.Add(people);
                    }
                    #endregion
                   
                    rep.Add<PeopleSemih>(peopleSemih, false);
                    var notIn = list.Where(m => !saleIdList.Contains(m.SALEID)).ToList(); //获取没有经销商的中奖纪录
                                                                                          //半点统计加入其他数据
                   
                   
                    var otherPeopleSemih = new PeopleSemih
                    {
                        PROVINCE = "其他",
                        TYPE = i + 1,
                        COUNT = notIn.Select(m => m.OPENID).Distinct().Count(),
                        ROW_UPDATE_DATE = yesterday
                    };
                    rep.Add<PeopleSemih>(otherPeopleSemih, false);
                    dayOpenid.AddRange(notIn.Select(r => new OpenIdViewModel
                    {
                        Provice = "其他",
                        OpenId = r.OPENID
                    }));
                    

                    //半点统计加入全国数据
                    var totalPeopleSemih = new PeopleSemih
                    {
                        PROVINCE = "全国",
                        TYPE = i + 1,
                        //COUNT = peopleSemih.Select(m => m.COUNT).Sum() + otherPeopleSemih.COUNT,
                        COUNT = list.Select(m => m.OPENID).Distinct().Count(),
                        ROW_UPDATE_DATE = yesterday
                    };
                    rep.Add<PeopleSemih>(totalPeopleSemih, false);
                    dayPeopleSemih.AddRange(peopleSemih);
                    dayPeopleSemih.Add(otherPeopleSemih);}

                #endregion

              

                #region  红包方法次数、红包金额、参与人数、积分数, 天、周、月统计

             
                var peopleDay = new List<PeopleDay>(); //参与人数天数表
                var totalProvince = new List<string> { "其他" };
                totalProvince.AddRange(provinceList);
                foreach (var n in totalProvince)
                {
                    //var provinceDayPeople = dayPeopleSemih.Where(m => m.PROVINCE == n).ToList();
                    var provinceDayPeople = dayOpenid.Where(m => m.Provice == n).ToList();
                
                    peopleDay.Add(new PeopleDay
                    {
                        PROVINCE = n,
                        COUNT = provinceDayPeople.Select(m => m.OpenId).Distinct().Count(),
                        ROW_UPDATE_DATE = yesterday
                    });
                  
                   
                    var peopleWeek =
                        rep.Find<PeopleWeek>(
                            m =>
                                m.CURRENT_WEEK == weekOfYear && m.PROVINCE == n && yesterday >= m.WEEK_BEGIN &&
                                yesterday < m.WEEK_END);
                    if (isFirstDay) //判断是否是每周第一天, 当前省份周数据统计
                    {
                       
                        peopleWeek = new PeopleWeek
                        {
                            COUNT = provinceDayPeople.Select(m => m.OpenId).Distinct().Count(),
                            WEEK_BEGIN = yesterday,
                            CURRENT_WEEK = weekOfYear,
                            ROW_UPDATE_DATE = yesterday,
                            PROVINCE = n
                        };
                        peopleWeek.WEEK_END = peopleWeek.WEEK_BEGIN.AddDays(6);
                        rep.Add<PeopleWeek>(peopleWeek, false);

                   
                    }
                    else
                    {
                       

                        if (peopleWeek == null)
                        {
                            peopleWeek = new PeopleWeek
                            {
                                COUNT = provinceDayPeople.Select(m => m.OpenId).Distinct().Count(),
                                WEEK_BEGIN = yesterday.AddDays(1 - dayOfWeek),
                                CURRENT_WEEK = weekOfYear,
                                ROW_UPDATE_DATE = yesterday,
                                PROVINCE = n
                            };
                            peopleWeek.WEEK_END = peopleWeek.WEEK_BEGIN.AddDays(6);
                            rep.Add<PeopleWeek>(peopleWeek, false);
                        }
                        else
                        {
                            peopleWeek.COUNT += provinceDayPeople.Select(m => m.OpenId).Distinct().Count();
                            rep.Update<PeopleWeek>(peopleWeek, false);
                        }
                    }
                    
                    var peopleMonth =
                        rep.Find<PeopleMonth>(
                            m =>
                                m.PROVINCE == n && m.CURRENT_MONTH == currMonth && yesterday >= m.MONTH_BEGIN &&
                                yesterday < m.MONTH_END);
                    
                    if (isFirstDayOfMonth) //判断昨天是否是当前省当月第一天,当前月数据统计
                    {
                        
                       

                        peopleMonth = new PeopleMonth
                        {
                            PROVINCE = n,
                            COUNT = provinceDayPeople.Select(m => m.OpenId).Distinct().Count(),
                            MONTH_BEGIN = yesterday,
                            MONTH_END = yesterday.AddMonths(1).AddDays(-1),
                            ROW_UPDATE_DATE = yesterday,
                            CURRENT_MONTH = yesterday.Month
                        };
                        rep.Add<PeopleMonth>(peopleMonth, false);

                       
                    }
                    else
                    {
                       
                       

                        if (peopleMonth == null)
                        {
                            peopleMonth = new PeopleMonth
                            {
                                PROVINCE = n,
                                COUNT = provinceDayPeople.Select(m => m.OpenId).Distinct().Count(),
                                MONTH_BEGIN = yesterday.AddDays(1 - yesterday.Day),
                                ROW_UPDATE_DATE = yesterday,
                                CURRENT_MONTH = yesterday.Month
                            };
                            peopleMonth.MONTH_END = peopleMonth.MONTH_BEGIN.AddMonths(1).AddDays(-1);
                            rep.Add<PeopleMonth>(peopleMonth, false);
                        }
                        else
                        {
                            peopleMonth.COUNT += provinceDayPeople.Select(m => m.OpenId).Distinct().Count();
                            rep.Update<PeopleMonth>(peopleMonth, false);
                        }
                    }
                }
                #endregion
                #region 全国数据统计

               
                int weekProvincePeople = dayOpenid.Select(m => m.OpenId).Distinct().Count(); //当天用户数
              
              
                //为全国统计每天人数
                var totalPeopleDay =
                    rep.Find<PeopleDay>(m => m.PROVINCE == "全国" && m.ROW_UPDATE_DATE == yesterday);
                if (totalPeopleDay != null)
                {
                    totalPeopleDay.COUNT += weekProvincePeople;
                    rep.Update(totalPeopleDay, false);
                }
                else
                {
                    var newTotalPeopleDay = new PeopleDay
                    {
                        PROVINCE = "全国",
                        ROW_UPDATE_DATE = yesterday,
                        COUNT = weekProvincePeople
                    };
                    peopleDay.Add(newTotalPeopleDay);
                }
              
             

                //统计全国当前周人数数据
                var totalWeekPeople =
                    rep.Find<PeopleWeek>(m => m.PROVINCE == "全国" && m.CURRENT_WEEK == weekOfYear);
                if (totalWeekPeople == null)
                {
                    var newTotalWeekPeople = new PeopleWeek
                    {
                        PROVINCE = "全国",
                        COUNT = weekProvincePeople,
                        ROW_UPDATE_DATE = yesterday,
                        WEEK_BEGIN = yesterday.AddDays(1 - dayOfWeek),
                        WEEK_END = yesterday.AddDays(6),
                        CURRENT_WEEK = weekOfYear
                    };
                    newTotalWeekPeople.WEEK_END = newTotalWeekPeople.WEEK_BEGIN.AddDays(6);
                    rep.Add<PeopleWeek>(newTotalWeekPeople, false);
                }
                else
                {
                    totalWeekPeople.COUNT += weekProvincePeople;
                    rep.Update<PeopleWeek>(totalWeekPeople, false);
                }
             


                //统计全国当前月参与人次数据
                var totalMonthPeople =
                    rep.Find<PeopleMonth>(m => m.PROVINCE == "全国" && m.CURRENT_MONTH == currMonth);
                if (totalMonthPeople == null)
                {
                    var newTotalMonthPeople = new PeopleMonth
                    {
                        PROVINCE = "全国",
                        COUNT = weekProvincePeople,
                        ROW_UPDATE_DATE = yesterday,
                        MONTH_BEGIN = yesterday.AddDays(1 - yesterday.Day),
                        CURRENT_MONTH = currMonth
                    };
                    newTotalMonthPeople.MONTH_END = newTotalMonthPeople.MONTH_BEGIN.AddMonths(1).AddDays(-1);
                    rep.Add<PeopleMonth>(newTotalMonthPeople, false);
                }
                else
                {
                    totalMonthPeople.COUNT += weekProvincePeople;
                    rep.Update<PeopleMonth>(totalMonthPeople, false);
                }
                

                #endregion

              
                var peopleDayFlag = rep.Add<PeopleDay>(peopleDay);
                

              
                count = peopleDay.Count;
           
            var endDate = DateTime.Now;
            return count;
        }
    }
}